package com.sumainfor.common.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.*;

/**
 * 唐巢&果加智能门锁
 * 果加智能门锁工具类
 * @ClassName: InteGuoJiaUtil
 * @author xzzhao
 * @date 2019年3月11日
 */
public class InteConchUtil {
    static Logger  log = LoggerFactory.getLogger(InteConchUtil.class);// 日志

    /**
     * 使用SHA256  返回签名
     * @param params
     * @param appkey
     * @return
     */
    public static String sign(Map<String, String> params,String appkey) {
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        keys.forEach(k->sb.append(k).append(params.get(k)));
        sb.append(appkey);
        System.out.println("字符串:"+sb);
        return DigestUtils.sha256Hex(sb.toString());
    }
    /**
     *登陆获取token
     * @return
     */
    public static Map<String,Object> login(Map<String,Object> params, JSONObject bodyJson) {
        // 装配请求参数
        String url=InteGuoJiaConfig.HOST+"/login";
        //发送请求
        Map<String,Object> map=HttpUtilBee.doPost(url,params,bodyJson);
        log.info(map.toString());
        return map;
    }
    public static String binary(byte[] bytes, int radix){
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }
    public static void main(String[] args) {
        /*返回签名*/
        Map<String,String> params=new HashMap<String,String>();
        params.put("appId",InteConchConfig.APPID);
        params.put("password",InteConchConfig.PASSWORD);
        params.put("userName",InteConchConfig.USER_NAME);
        System.out.println("签名1:"+InteConchUtil.sign(params,InteConchConfig.APPKEY));
        System.out.println("签名2:"+DigestUtils.sha256Hex("appIdoffline.tangchao.compasswordtangchaouserNameTCGY01401YjhiZGYzMWM5NGExYWJmMA=="));
    }
}